
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myqgraphicsview.h"
#include "graph.h"
#include "graph.cpp"
#include "interestNode.h"
#include <vector>

using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
    map()
{
    databasePreparation();

    ui->setupUi(this);
    //ui->mapview = new QGraphicsView;
    changelabel = new QTimer(this);
    connect(changelabel, &QTimer::timeout, this, &MainWindow::changePoiLabel);
    changelabel->start(100);

}


void MainWindow::databasePreparation()//数据库准备操作
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("Campus.db");//注意修改相对路径

    if (!db.open()) {
        qDebug() << "Failed to connect to database!";
    }

    qDebug() << "连接完成";
    QSqlQuery query;
    QString createTableQuery = "CREATE TABLE IF NOT EXISTS campus ("
                               "id INTEGER PRIMARY KEY AUTOINCREMENT)"
                               ;
    query.exec(createTableQuery);

}

void MainWindow::changePoiLabel(){
    QPointF poi = ui->mapview->getPoi();
    QString Poi = QString::number(poi.x())+","+QString::number(poi.y());
    ui->PoiLabel->setText(Poi);
}











MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_bigButton_clicked()
{
    ui->mapview->scale(1.3, 1.3);
}


void MainWindow::on_smallButton_clicked()
{
   ui->mapview->scale(1/1.3, 1/1.3);
}


void MainWindow::on_showButton_clicked()
{
   vector<QPointF> data;
   //从图中获取景点列表
   interestNode* vertex = map.getVertex();
   //把景点列表的坐标提取出来
   for(int i = 0;i<map.getVertexNum();i++){
        data.push_back(vertex[i].myPoi);
   }
   //把景点列表的坐标传输到map
   ui->mapview->setInterest(data);
   //在mao上打印景点
   ui->mapview->drawInterest();
}




void MainWindow::on_addPlaceButton_clicked()
{
   interestNode newData;
   newData.myPoi = ui->mapview->getPoi();
   qDebug()<<newData.myPoi;
   newData.name = ui->lineEdit->text();
   newData.id = map.getVertexNum()+1;
   newData.inf = "None";
   map.addVertex(newData);
   map.printEdge();
   map.printVertexPoi();
   vector<QPointF> data;
   interestNode* nowVertex = map.getVertex();
   for(int i = 0;i<map.getVertexNum();i++){
        interestNode nowData = nowVertex[i];
        data.push_back(nowData.myPoi);
   }
   ui->mapview->setInterest(data);
   ui->mapview->drawInterest();
}

